﻿using System.Windows;
using System.Windows.Media;

namespace Gavel.Common.Component.Extension;

/// <summary>
/// 视觉扩展类
/// </summary>
public static class VisualExtension
{
    /// <summary>
    /// 从系统属性获取目标泛型的子属性
    /// </summary>
    /// <param name="dependency">系统属性</param>
    /// <typeparam name="T">目标泛型</typeparam>
    /// <returns>目标泛型的子属性</returns>
    public static T? Get<T>(this DependencyObject dependency) where T : DependencyObject
    {
        if (dependency is T t)
        {
            return t;
        }

        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dependency); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(dependency, i);
            T? result = child.Get<T>();
            if (result != null)
            {
                return result;
            }
        }

        return null;
    }
}